package com.ruicar.afs.cloud.basic.common.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.basic.common.condition.RevokeHandleCondition;
import com.ruicar.afs.cloud.basic.common.entity.BasicBusinessAccounting;
import com.ruicar.afs.cloud.basic.common.entity.BasicMainInfo;
import com.ruicar.afs.cloud.basic.common.entity.BasicReceiptRecord;
import com.ruicar.afs.cloud.basic.common.entity.BasicRevokeHandle;
import com.ruicar.afs.cloud.basic.common.mapper.BasicRevokeHandleMapper;
import com.ruicar.afs.cloud.basic.common.service.BasicBusinessAccountingService;
import com.ruicar.afs.cloud.basic.common.service.BasicMainInfoService;
import com.ruicar.afs.cloud.basic.common.service.BasicRevokeHandleService;
import com.ruicar.afs.cloud.bizcommon.voucher.service.VoucherFlowService;
import com.ruicar.afs.cloud.common.core.enums.AfsEnumUtil;
import com.ruicar.afs.cloud.common.core.uid.UidGenerator;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.modules.contract.enums.FeeTypeEnum;
import com.ruicar.afs.cloud.common.modules.contract.enums.ProcessStatusEnum;
import com.ruicar.afs.cloud.common.modules.contract.enums.TreatmentMethodEnum;
import com.ruicar.afs.cloud.common.modules.constant.VoucherBuriedPointNo;
import com.ruicar.afs.cloud.common.modules.dto.mq.voucher.VoucherFlowInfoDto;
import com.ruicar.afs.cloud.common.modules.enums.BusinessType;
import com.ruicar.afs.cloud.common.util.EmptyUtils;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;

/**
 * @author infinity
 * @version 1.0
 * @date 2020/9/3 11:44
 */
@Service
@AllArgsConstructor
public class BasicRevokeHandleServiceImpl extends ServiceImpl<BasicRevokeHandleMapper, BasicRevokeHandle> implements BasicRevokeHandleService {

    private final BasicBusinessAccountingService basicBusinessAccountingService;
    private final BasicMainInfoService basicMainInfoService;
    private final VoucherFlowService voucherFlowService;
    private UidGenerator uidGenerator;

    @Override
    public void saveBasicRevokeHandle(BasicReceiptRecord receiptRecord) {
        BasicRevokeHandle handle = BasicRevokeHandle.convertFromDto(receiptRecord);
        this.save(handle);
    }

    @Override
    public IResponse getRevokeHandleList(RevokeHandleCondition condition) {
        return IResponse.success(this.baseMapper.getRevokeHandleList(new Page(condition.getPageNumber(), condition.getPageSize()), condition));
    }

    @Override
    public IResponse revokeHandleAgree(Long receiptId) {
        BasicRevokeHandle basicRevokeHandle = this.getOne(Wrappers.<BasicRevokeHandle>lambdaQuery().eq(BasicRevokeHandle::getReceiptId, receiptId), false);
        if (EmptyUtils.isNotEmpty(basicRevokeHandle)) {
            Assert.isTrue((!ProcessStatusEnum.draft.equals(basicRevokeHandle.getProcessStatus())
                    && !ProcessStatusEnum.refuse.equals(basicRevokeHandle.getProcessStatus())), "该数据已在流程中！");
            basicRevokeHandle.setProcessStatus(ProcessStatusEnum.approved);
            basicRevokeHandle.setEndTime(Calendar.getInstance().getTime());
            this.updateById(basicRevokeHandle);
            if (TreatmentMethodEnum.REFUND.equals(basicRevokeHandle.getTreatmentMethod())) {
                handleReturn(basicRevokeHandle);
            }
            return IResponse.success("处理成功");
        } else {
            return IResponse.fail("合同系统中无此撤销id");
        }
    }

    //预缴款退回处理
    @Override
    public void handleReturn(BasicRevokeHandle basicRevokeHandle) {
        BasicBusinessAccounting basicBusinessAccounting = new BasicBusinessAccounting();
        basicBusinessAccounting.setBuriedPointNo(VoucherBuriedPointNo.advancePaymentRetrun);
        basicBusinessAccounting.setTransId(uidGenerator.getUID());
        basicBusinessAccounting.setFeeType(AfsEnumUtil.key(FeeTypeEnum.unusedFund));
        basicBusinessAccounting.setAmount(basicRevokeHandle.getArriveAmt());
        basicBusinessAccounting.setKeepAccountDate(new Date());
        basicBusinessAccounting.setContractNo(basicRevokeHandle.getContractNo());
        basicBusinessAccountingService.save(basicBusinessAccounting);
        BasicMainInfo mainInfo = basicMainInfoService.getOne(Wrappers.<BasicMainInfo>lambdaUpdate().eq(BasicMainInfo::getContractNo, basicRevokeHandle.getContractNo()));
        //凭证埋点
        VoucherFlowInfoDto voucherFlowInfoDto = new VoucherFlowInfoDto();
        voucherFlowInfoDto.setTransNo(String.valueOf(basicBusinessAccounting.getTransId()));
        voucherFlowInfoDto.setContractNo(mainInfo.getContractNo());
        voucherFlowInfoDto.setBuriedPointNo(VoucherBuriedPointNo.advancePaymentRetrun);// 埋点编号
        voucherFlowInfoDto.setDealerName(mainInfo.getChannelFullName());
        voucherFlowInfoDto.setTransNo(mainInfo.getContractNo());
        voucherFlowInfoDto.setKeepAccountDate(mainInfo.getStartDate());
        voucherFlowInfoDto.setBusinessType((BusinessType) AfsEnumUtil
                .getEnum(AfsEnumUtil.key(mainInfo.getBusinessType()), BusinessType.class));
        voucherFlowService.sendFlow(voucherFlowInfoDto);
    }


}
